package com.banciyuan.bcywebview.biz.circles.relatedcircle;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.TagDetail;

public class RelatedCircleAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<TagDetail> mlist = new ArrayList<>();
    private ImageLoader mImageLoader;

    public RelatedCircleAdapter(Context context, List<TagDetail> mlist) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mlist = mlist;
        mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TagDetail tagDetail = mlist.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.circle_list_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        mImageLoader.displayImage(tagDetail.getCover(), viewHolder.img, ImageOptions.options);
        if ("work".equals(tagDetail.getType())) {
            viewHolder.name.setText(tagDetail.getName());
        } else {
            viewHolder.name.setText(tagDetail.getTag_name());
        }
        if (!TextUtils.isEmpty(tagDetail.getIntro()))
            viewHolder.intro.setText(Html.fromHtml(tagDetail.getIntro()));
        return convertView;
    }

    public void setdata(List<TagDetail> tags) {
        mlist = tags;
    }

    private class ViewHolder {
        public ImageView img;
        public TextView name;
        public TextView intro;

        public ViewHolder(View convertView) {
            img = (ImageView) convertView.findViewById(R.id.iv_work);
            name = (TextView) convertView.findViewById(R.id.tv_workname);
            intro = (TextView) convertView.findViewById(R.id.tv_workintro);
        }
    }
}
